//
//  ViewController.m
//  DriverBus
//
//  Created by MacMini20170218 on 2017/3/26.
//  Copyright © 2017年 JiYang. All rights reserved.
//

#import "LoginInViewController.h"
#import "MyMD5Util.h"
#import "NetWorking.h"
#import "JRToast.h"
#import "StartViewController.h"
#import "AppDelegate.h"

@interface LoginInViewController ()

@end

@implementation LoginInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.phoneField resignFirstResponder];
    [self.passField resignFirstResponder];
    [self.loadingView setHidden:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


- (IBAction)clickLoginButton:(id)sender {
    [self login];
}

- (IBAction)touchDownEmpty:(id)sender {
    [self.phoneField resignFirstResponder];
    [self.passField resignFirstResponder];
    
}

- (IBAction)passInputDone:(id)sender {
    [self login];
}

- (void)login{
    if(![NetWorking netConnect]){
        UIAlertView *alertNet = [[UIAlertView alloc]initWithTitle:@"网络错误" message:@"网络未连接，请检查网络连接." delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        alertNet.alertViewStyle = UIAlertViewStyleDefault;
        [alertNet show];
        return;
    }
    [self.passField resignFirstResponder];
    NSString *phone = self.phoneField.text;
    NSString *pass = self.passField.text;
    if(phone.length==0||pass.length==0){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"输入错误" message:@"输入不能为空，请检查输入" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        alert.alertViewStyle = UIAlertViewStyleDefault;
        [alert show];
        return;
    }
    if(phone.length!=11){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"手机号错误" message:@"手机号位数错误，请检查输入" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        alert.alertViewStyle = UIAlertViewStyleDefault;
        [alert show];
        return;
    }
    pass = [MyMD5Util MD5:pass];
    [self loginWith:phone pass:pass];
}

- (void)loginWith:(NSString*)phone pass:(NSString *)pass{
    [self.loadingView setHidden:false];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setObject:phone forKey:@"dri_account"];
    [dict setObject:pass forKey:@"dri_password"];
    NSString * loginUrl = @"http://122.114.61.173/bus/index.php/Home/DriLogin/login";
    AFHTTPSessionManager *manage = [NetWorking manager];
    [manage POST:loginUrl parameters:dict
        progress:^(NSProgress * _Nonnull uploadProgress) {
        }
         success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
             [self.loadingView setHidden:YES];
             if(responseObject){
                 NSDictionary* result = responseObject;
                 NSString* status = [result objectForKey:@"login_code"];
                 AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
                 if([status isEqualToString:@"1"]){
                     //登录成功
                     [JRToast showWithText:@"登录成功"];
                     appDelegate.driver.name = [result objectForKey:@"dri_name"];
                     appDelegate.driver.number = [result objectForKey:@"jub_num"];
                     appDelegate.driver.sex = [result objectForKey:@"dri_sex"];
                     appDelegate.driver.imageUrl = [result objectForKey:@"head_address"];
                     appDelegate.driver.account = phone;
                     appDelegate.driver.pass = pass;
                     //保存已经登录和用户信息
                     [appDelegate.driver saveToData:YES];
                     //跳转到主界面
                     [self performSegueWithIdentifier:@"LoginSucessSegue" sender:self];
                 }else{
                     //登录失败
                     [JRToast showWithText:@"登录失败，请重试"];
                     [appDelegate.driver saveToData:NO];
                 }
             }
         }
         failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
             [self.loadingView setHidden:YES];
             if(error){
                 [JRToast showWithText:[NSString stringWithFormat:@"登录出错:%@",error] bottomOffset:50.0f];
             }
         }];
    
}

@end
